$(function() {
	init();

});




function successCallBack() {
	window.location.href = 'gameList.jsp';
}

/**
 * 提示信息处理
 */
function savehandle() {
	$('#alertdiv').hide();
	$('#alertdiv').savehandle( {
		fieldMap : {},
		successMsg : '添加成功！',
		failMsg : '添加失败！错误：',
		success : successCallBack
	});
}

//图片上传
function upload(){
    $.ajaxFileUpload({
        url: '/fw/upload/imageUpload.action', //用于文件上传的服务器端请求地址
        secureuri: false, //是否需要安全协议，一般设置为false
        fileElementId: 'upfile', //文件上传域的ID
        dataType: 'json', //返回值类型 一般设置为json
        success: function (data, status){  //服务器成功响应处理函数
        	var json = eval('('+data+')');
        	var html = "<img style='width:200px;height:150px;' src='"+json.url+"'/>";
        	$("#upimg").html(html);
        	$("#imgUrl").val(json.url);
        	$('#alertdiv').savehandle('setmsg',{msg:'上传成功！！',timer:2000});
        },
        error: function (data, status, e)//服务器响应失败处理函数
        {
        	$('#alertdiv').savehandle('setmsg',{msg:'上传失败！！',timer:2000});
        }
    });
    return false;
}



function init() {
	savehandle();
	changeActiveItem('gameList');
}

function add1() {
	$("#gameAddForm").ajaxSubmit({
		url: '/fw/game/game_add.action',
		data: {},
		beforeSubmit: function() {
			return validateTooltip([{
				inputSel: '#gameName',
				validStr: 'empty'
			}, {
				inputSel: '#gameLevel',
				validStr: 'empty'
			},{
				inputSel: '#imgUrl',
				validStr: 'empty'
			}, {
				inputSel: '#useGrade',
				validStr: 'empty'
			}, {
				inputSel: '#sortOrder',
				validStr: 'empty'
			}, {
				inputSel: '#fixed',
				validStr: 'empty'
			}, {
				inputSel: '#intro',
				validStr: 'empty'
			}]);
		},
		success: function(r) {
			var data = $.parseJSON(r);
			if (data.success) {
				$MyAlert.alert('成功!', successCallBack);
			} else {
				$MyAlert.error(msg.errors);
			}
		}
	});
}

function add() {
	var gameName = $('#gameName').val();
	if (!gameName) {
		$('#alertdiv').savehandle('setmsg', {msg : '请填写游戏标题!',timer : 2000});
		return;
	}
	var gameLevel = $('#gameLevel').val();
	if (!gameLevel) {
		$('#alertdiv').savehandle('setmsg', {msg : '请选择奖品等级!',timer : 2000});
		return;
	}
	var useGrade = $('#useGrade').val();
	if (!useGrade) {
		$('#alertdiv').savehandle('setmsg', {msg : '请填写消耗积分!',timer : 2000});
		return;
	}
	var gameTime = $('#gameTime').val();
	if (!gameTime) {
		$('#alertdiv').savehandle('setmsg', {msg : '请填写游戏次数!',timer : 2000});
		return;
	}
	var fixed = $('#fixed').val();
	if (!fixed) {
		$('#alertdiv').savehandle('setmsg', {msg : '请填写中奖基数!',timer : 2000});
		return;
	}
	var sortOrder = $('#sortOrder').val();
	if (!sortOrder) {
		$('#alertdiv').savehandle('setmsg', {msg : '请填写排列序号!',timer : 2000});
		return;
	}
	var imgUrl = $('#imgUrl').val();
	if (!imgUrl) {
		$('#alertdiv').savehandle('setmsg', {msg : '请上传游戏图片!',timer : 2000});
		return;
	}
	var intro = $('#intro').val();
	if (!intro) {
		$('#alertdiv').savehandle('setmsg', {msg : '请填写游戏介绍!',timer : 2000});
		return;
	}
	var data = {
		"gameName" : gameName,
		"gameLevel" : gameLevel,
		"useGrade" : useGrade,
		"gameTime" : gameTime,
		"fixed" : fixed,
		"imgUrl" : imgUrl,
		"intro" : intro,
		"sortOrder" : sortOrder,
		"push" : false
	};
	$.getJSON('/fw/game/game_add.action', data, function(data) {
		if (data.success) {
			$('#alertdiv').savehandle('setmsg', {
				msg : '成功!',
				timer : 2000
			});
			successCallBack();
		} else {
			var msg = data.result;
			$('#alertdiv').savehandle('setmsg', {
				msg : msg.errors,
				timer : 2000
			});
		}
	});
}
